package com.example.demo.util;

import java.awt.*;
import java.util.Arrays;

/**
 * marketting-cloud
 * berry
 * 2020/12/21
 */
public class Dijkstra {
    static int[][] sortDistance = {{0, 0}, {1, 999}, {2, 999}, {3, 999}, {4, 999}, {5, 999}};

    public static point[] generater() {

        point p0 = new point(0, new int[][]{{1, 1}, {2, 4}});

        point p1 = new point(1, new int[][]{{0, 1}, {2, 2}, {3, 7}, {4, 5}});

        point p2 = new point(2, new int[][]{{0, 4}, {1, 2}, {4, 1}});

        point p3 = new point(3, new int[][]{{1, 7}, {4, 3}, {5, 2}});

        point p4 = new point(4, new int[][]{{2, 1}, {1, 5}, {3, 3}, {5, 6}});

        point p5 = new point(5, new int[][]{{3, 2}, {4, 6}});

        return new point[]{p0, p1, p2, p3, p4, p5};


    }

    public static void count(point[] pointArr) throws Exception {

        for (point p : pointArr) {
            int[][] NeiDistace = p.getNeighbor();


            for (int[] ints : NeiDistace) {

                int neiDis = ints[1];
                int neiName = ints[0];
                int sum = sortDistance[p.getNumber()][1] + neiDis;
                int exitNeiSortLen = sortDistance[neiName][1];


                if (sum < exitNeiSortLen) {
                    sortDistance[neiName][1] = sum;
                }
                if (exitNeiSortLen + neiDis < sortDistance[p.getNumber()][1]) {
                    sortDistance[p.getNumber()][1] = exitNeiSortLen + neiDis;
                }
            }
        }
    }


    public static void main(String[] args) throws Exception {
        count(generater());
        for (int[] ints : sortDistance) {
            System.out.println(ints[0] + "   distance to starting point   " + ints[1]);
        }
    }
}

class point {
    private int number;

    private int[][] neighbor;



    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int[][] getNeighbor() {
        return neighbor;
    }

    public void setNeighbor(int[][] neighbor) {
        this.neighbor = neighbor;
    }



    public point(int number, int[][] neighbor) {
        this.setNumber(number);
        this.setNeighbor(neighbor);
    }
}
